<template>
  <smart-page>
    <template #actions>
      <a-button   v-auth="Acl.SYSTEM_API_DOWNLOAD" @click="onDownLoad()">下载权限表达式</a-button>
      <a-button   v-auth="Acl.SYSTEM_API_SCAN" @click="onScan">扫描接口</a-button>
      <a-button   v-auth="Acl.SYSTEM_API_DELETE" :disabled="selectedKeys.length === 0" @click="onDelete()">批量删除</a-button>
      <a-button   v-auth="Acl.SYSTEM_API_ADD" type="primary" @click="editCtr.open()">新建权限</a-button>
    </template>
    <template #search>
      <smart-search :query-array="API_SEARCH" @on-handler="onSearch"/>
    </template>
    <a-table v-model:selectedKeys="selectedKeys"
             :table-layout-fixed="theme.layoutFixed"
             :row-selection="rowSelection"
             :columns="API_COLUMNS"
             :row-key="rowKey"
             :column-resizable="theme.resizable"
             :bordered="theme.bordered"
             :stripe="theme.stripe"
             :sticky-header="theme.sticky"
             :hoverable="theme.hoverAble"
             :pagination="pagination"
             :loading="theme.loading"
             :data="dataSource"
             @page-change="onPageChange"
             @page-size-change="onPageSizeChange"
    >

      <template #action="{ record }">
        <a-space>
          <a-link v-auth="Acl.SYSTEM_API_UPDATE" @click="editCtr.open(record)"> 编辑</a-link>
          <a-link v-auth="Acl.SYSTEM_API_DELETE" status="danger" @click="onDelete(record)"> 删除</a-link>
        </a-space>
      </template>
    </a-table>


    <edit-modal v-if="editCtr.show" :record="editCtr.record" @cancel="editCtr.close"/>

  </smart-page>


</template>
<script lang="ts" setup name="system-settings-api">
import {onMounted} from 'vue';
import {API_SEARCH} from '../resource/search'
import {API_COLUMNS} from "../resource/colums";
import {TablePage} from "@/libs/core";
import {SystemHttp} from "../resource/http";
import EditModal from './edit.vue'
import {MsgUtil} from "@/utils";
import {saveAs} from "file-saver";
import {Acl} from "@/libs/acl";


const {getApiPage, delApi, scanApi} = SystemHttp
const pageConfig = {title: '权限', rowKey: 'id', nameKey: 'title'}
const tablePage = new TablePage(pageConfig).setFetchXhr(getApiPage)
    .setPermission(Acl.SYSTEM_API_PAGE_LIST)
    .setDeleteXhr(delApi);

const {theme, pagination, rowKey, dataSource, rowSelection, selectedKeys, editCtr} = tablePage
const {onFetch, onPageChange, onPageSizeChange, onDelete, onSearch} = tablePage
onMounted(() => onFetch())

const onScan = () => {
  const tips = "确定要重新扫描前档系统接口权限吗?"
  const handler = async () => {
    const {message} = await scanApi();
    MsgUtil.notify('操作成功', message, 'success');
    await onFetch()
  }
  MsgUtil.confirm(tips, handler)
}


const onDownLoad = () => {
  const tips = "确定要下载所有的权限表达式吗?"

  const handler = async () => {
    const {message, data} = await SystemHttp.downLoadApi()
    if (data.length) {
      const blob = new Blob([data], {type: 'application/javascript'})
      saveAs(blob, 'acl.ts')
    } else {
      MsgUtil.notify('操作失败', message, 'error')
    }

  }

  MsgUtil.confirm(tips, handler)

}
</script>

